package org.yenn.pattern.aliyun.structural.flyweight;

import org.junit.Test;
import org.yenn.pattern.aliyun.structural.flyweight.impl.Circle;

/**
 * 步骤 4
 * 使用该工厂，通过传递颜色信息来获取实体类的对象。
 */
public class FlyweightPatternDemo {
    private static final String colors[] =  { "Red", "Green", "Blue", "White", "Black" };

    private static String getRandomColor() {
        return colors[(int)(Math.random()*colors.length)];
    }
    private static int getRandomX() {
        return (int)(Math.random()*100 );
    }
    private static int getRandomY() {
        return (int)(Math.random()*100);
    }
    /**
     *
     * 步骤 5
     * 验证输出。
     * Creating circle of color : Green
     * Circle: Draw() [Color : Green, x : 46, y :23, radius :100
     * Creating circle of color : White
     * Circle: Draw() [Color : White, x : 30, y :52, radius :100
     * Circle: Draw() [Color : White, x : 86, y :19, radius :100
     * Creating circle of color : Blue
     * Circle: Draw() [Color : Blue, x : 4, y :57, radius :100
     * Creating circle of color : Red
     * Circle: Draw() [Color : Red, x : 19, y :68, radius :100
     * Circle: Draw() [Color : Red, x : 50, y :95, radius :100
     * Circle: Draw() [Color : Blue, x : 83, y :63, radius :100
     * Circle: Draw() [Color : Green, x : 81, y :57, radius :100
     * Circle: Draw() [Color : White, x : 42, y :17, radius :100
     * Circle: Draw() [Color : White, x : 96, y :93, radius :100
     * Circle: Draw() [Color : Green, x : 58, y :6, radius :100
     * Circle: Draw() [Color : White, x : 48, y :25, radius :100
     * Circle: Draw() [Color : Green, x : 30, y :13, radius :100
     * Circle: Draw() [Color : Blue, x : 57, y :84, radius :100
     * Circle: Draw() [Color : Blue, x : 71, y :77, radius :100
     * Circle: Draw() [Color : Red, x : 92, y :90, radius :100
     * Circle: Draw() [Color : Green, x : 56, y :99, radius :100
     * Circle: Draw() [Color : Red, x : 97, y :3, radius :100
     * Circle: Draw() [Color : Red, x : 46, y :47, radius :100
     * Circle: Draw() [Color : Red, x : 74, y :84, radius :100
     *
     * @return
     */
    @Test
    public void getCircle() {

        for(int i=0; i < 20; ++i) {
            Circle circle =   (Circle)ShapeFactory.getCircle(getRandomColor());
            circle.setX(getRandomX());
            circle.setY(getRandomY());
            circle.setRadius(100);
            circle.draw();
        }
    }
}